import qrcode
from PIL import Image, ImageFont, ImageDraw
import zipfile
import os


def make_qrcode(url, path, zip_name, use_logo=False, target_path='qrcode', remove_png=False, show_text=False):
    qr = qrcode.QRCode(
        version=4,
        # 设置容错率为最高
        error_correction=qrcode.ERROR_CORRECT_Q,
        box_size=11,
        border=3,
    )
    qr.add_data(url)
    qr.make(fit=True)
    img = qr.make_image()

    if use_logo:
        # 设置二维码为彩色
        img = img.convert("RGBA")
        icon = Image.open('logo.png')
        w, h = img.size
        factor = 4
        size_w = int(w / factor)
        size_h = int(h / factor)
        icon_w, icon_h = icon.size
        if icon_w > size_w:
            icon_w = size_w
        if icon_h > size_h:
            icon_h = size_h
        icon = icon.resize((icon_w, icon_h), Image.ANTIALIAS)
        w = int((w - icon_w) / 2)
        h = int((h - icon_h) / 2)
        icon = icon.convert("RGBA")
        newimg = Image.new("RGBA", (icon_w + 8, icon_h + 8), (255, 255, 255))
        img.paste(newimg, (w - 4, h - 4), newimg)
        img.paste(icon, (w, h), icon)

    # add a text
    # newimg = Image.new("RGBA", (100, 100), (255, 255, 255))
    if show_text:
        img = make_text_image(img, path)

    img_name = target_path + "/" + path + '.png'
    img.save(img_name, quality=100)
    f = zipfile.ZipFile(target_path + "/" + zip_name + '.zip', 'a')
    f.write(img_name)
    f.close()

    if remove_png:
        os.remove(img_name)


def make_text_image(img, name):
    text_area_height = 88

    text_size = 42
    text_img = Image.new('RGBA', (img.size[0], text_area_height), (255, 255, 255))
    draw = ImageDraw.Draw(text_img)
    # width, height = text_img.size
    draw.text((28, 0), '蓝牙名称: %s' % name, font=ImageFont.truetype(u"pf.ttc", text_size), fill='#000000')
    draw.text((28, text_size + 5), '蓝牙密码:', font=ImageFont.truetype(u"pf.ttc", text_size), fill='#000000')
    draw.text((28 + text_size * 4.5, text_size + 8), '0000', font=ImageFont.truetype(u"pf.ttc", text_size + 1),
              fill='#000000')

    img = img.convert("RGBA")
    # img.paste(text_img)
    # w, h = img.size
    width = int(img.size[0])
    height = int(img.size[1] + text_area_height)

    new_img = Image.new('RGBA', (width, height), (255, 255, 255))
    new_img.paste(img)
    new_img.paste(text_img, box=(0, img.height - 20))
    return new_img


qrcode_path = 'qrcode_printer'
exists = os.path.exists(qrcode_path)
if not exists:
    print('创建目录' + qrcode_path)
    os.makedirs(qrcode_path)


def create_code_images():
    names = []
    for i in range(1, 11):
        n = ""
        if i < 10:
            n = "0%d" % i
        else:
            n = "%d" % i
        names.append("SXW-400%s" % n)
    names.append("BJJY-1588")
    qrcode_zip = "qrcode/qrcode.zip"
    if os.path.exists(qrcode_zip):
        os.remove(qrcode_zip)
    for name in names:
        content = "type:6;name:%s" % name
        filename = name
        make_qrcode(content, filename, "qrcode", target_path=qrcode_path)


# create_code_images()

def create_printer_images():
    names = []
    for i in range(101, 132):
        n = i
        # if i < 10:
        #     n = "0%d" % i
        # else:
        #     n = "%d" % i

        names.append("sxw-p%d" % n)

    qrcode_zip = "%s/qrcode.zip" % qrcode_path
    if os.path.exists(qrcode_zip):
        os.remove(qrcode_zip)
    for name in names:
        content = "type:5;name:%s;pwd:0000" % name
        filename = name
        make_qrcode(content, filename, "qrcode", target_path=qrcode_path, show_text=True)


create_printer_images()
